package com.zhuss.meeting.processor;

import com.zhuss.bean.bo.meeting.apply.CheckApplyBo;
import com.zhuss.bean.bo.meeting.room.SearchRoomBo;
import com.zhuss.bean.vo.meeting.room.ListRoomVo;
import com.zhuss.common.PageVo;
import com.zhuss.exception.WpsException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.meeting.db.dao.ext.MeetingRoomDao;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: zhushanshuo
 * @Date: 2020/2/25 14:37
 */
@Component("roomRead")
public class RoomRead {

    @Autowired
    private MeetingRoomDao roomDao;


    /**
     * 会议室列表
     * @param bo
     * @return
     */
    public Object list(SearchRoomBo bo) {
        PageVo<ListRoomVo> result = new PageVo<ListRoomVo>();
        result.setPageNum(bo.getPageNum());
        result.setPageSize(bo.getPageSize());
        result.setItems(roomDao.list(bo));
        result.setTotal(roomDao.countList(bo));
        return result;
    }

    /**
     * 会议室详情
     * @param roomId
     * @throws WpsException
     */
    @SuppressWarnings("unchecked")
    public Object detail(Long roomId) throws WpsException {
        Object result = roomDao.detail(roomId);
        if (result == null) {
            throw new WpsException(ExceptionCode.数据不存在);
        }
        return result;
    }

    /**
     * 获取可用会议室
     * @param bo
     * @return
     */
    public Object checkList(CheckApplyBo bo) {
        PageVo<ListRoomVo> result = new PageVo<ListRoomVo>();
        result.setPageNum(bo.getPageNum());
        result.setPageSize(bo.getPageSize());
        Map<String, Object> map = new HashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date StartTime = bo.getStartTime();
        Date EndTime = bo.getEndTime();
        String meetingStartTime = sdf.format(DateUtils.addHours(StartTime,0-2));
        String meetingEndTime = sdf.format(DateUtils.addHours(EndTime,0+2));
        map.put("meetingStartTime",meetingStartTime);
        map.put("meetingEndTime",meetingEndTime);
        map.put("area",bo.getArea());
        result.setItems(roomDao.checkList(map));
        result.setTotal(roomDao.countCheckList(map));
        return result;
    }

}
